import instance from "../../utils/util"

// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text:"",
list:[],
    hdata:[],
    iszk:false
  },
  dk(){
this.setData({
    iszk:true
})
  },
  cz(){
    this.setData({
        iszk:false
    })
  },
  qd(){
    this.setData({
        iszk:false
    })
  },
  search1(e){
      console.log(e)
      if(e.detail.value !==''){
        this.setData({
            text :e.detail.value
        })
      }else{
          this.setData({
              text:""
          })
          this.getdata()
      }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options.name){
          this.setData({
              text:options.name
          })
          this.getlist()
      }
 this.getdata()

  },
  fanhui(){
      wx.navigateBack()
  },
 async getlist(){
const res=await instance.get("/api/products?keyword="+this.data.text);
console.log(res)
this.setData({
    list:res.data.list
})
if (wx.getStorageSync('info')) {
    let data = JSON.parse(wx.getStorageSync('info'))
    const ress = await instance.get("/api/user-favorites?userId=" + data.id);
    console.log(ress)

    console.log(this.data.new, "newss")
    let data2 = this.data.list;
    data2.forEach(el => {
        el.collect = false
        ress.data.forEach(ell => {
            if (el.id == ell.productId) {
                el.collect = true
            }
        })
    })
    console.log(data2)
    this.setData({
        list: data2,
        collect: ress.data
    })
}

  },
  getdata(){
    let data=[];
    if(wx.getStorageSync('list')){
      data=wx.getStorageSync('list')
    this.setData({
        hdata:data
    })
    }
  },
  remove(){
      wx.removeStorageSync('list')
      this.setData({
          hdata:[]
      })
      this.getdata()
  },
  todetail(e){
      wx.navigateTo({
        url: '/pages/shopinfo/shopinfo?id='+e.currentTarget.dataset.id,
      })
  },
  async collect(e) {
    console.log(e)
    let data = JSON.parse(wx.getStorageSync('info'))
    const res = await instance.post("/api/user-favorites", {
        userId: data.id,
        productId: e.currentTarget.dataset.id
    })
    this.getlist()
},
async collect1(e) {
    console.log(e)
    let data = JSON.parse(wx.getStorageSync('info'))
    let id=""
    this.data.collect.forEach(el=>{
        if(el.productId == e.currentTarget.dataset.id){
id=el.id
        }
    })
    const res = await instance.delete("/api/user-favorites/" + id+"?userId="+data.id,{
        userId:data.id
    });
    this.getlist()
},
  search(){
      let data=[];
      if(wx.getStorageSync('list')){
        data=wx.getStorageSync('list')
      
      }
      data.push(this.data.text)
      wx.setStorageSync('list', data)
      this.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})